/**
 * Copyright 2012 Pablo Danilo Aliscioni
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package ar.com.pabloaliscioni.cloud.core.configuration;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

/**
 * Represents the API's configuration deserialized form the XML configuration file
 * 
 * @author Pablo Danilo Aliscioni
 * @since 1.0
 */
@XStreamAlias("configuration")
public class Configuration implements Serializable
{
	/**
	 * Generated Serial Version UID
	 */
	private static final long serialVersionUID = -2804401061962341021L;
	
	/**
	 * Name of the provider to be used by default
	 */
	@XStreamAlias("defaultProvider")
   	@XStreamAsAttribute
	private String defaultProviderName;
	
	/**
	 * Set containing the configuration for the providers included in the XML configuration file
	 */
	@XStreamImplicit(itemFieldName="provider")
	private Set<ProviderConfiguration> providerConfigurations;
	
	/**
	 * Non-parameterized constructor
	 */
	public Configuration()
	{
		super();
	}
	
	/**
	 * Parameterized constructor
	 * 
	 * @param providerConfigurations Set containing the configuration for the providers included in the XML configuration file
	 * @param defaultProviderName Name of the provider to be used by default
	 */
	public Configuration(Set<ProviderConfiguration> providerConfigurations, String defaultProviderName)
	{
		super();
		
		this.setProviderConfigurations(providerConfigurations);
		this.setDefaultProviderName(defaultProviderName);
	}
	
	/**
	 * Gets the name of the provider to be used by default
	 * 
	 * @return The name of the provider to be used by default
	 */
	public String getDefaultProviderName()
	{
		return defaultProviderName;
	}
	/**
	 * Sets the name of the provider to be used by default
	 * 
	 * @param defaultProviderName The name of the provider to be used by default
	 * 
	 * @throws IllegalArgumentException If the provider name is invalid
	 */
	public void setDefaultProviderName(String defaultProviderName)
	{
		if(defaultProviderName == null || defaultProviderName.isEmpty())
			throw new IllegalArgumentException("The default provider name is invalid");
		
		this.defaultProviderName = defaultProviderName;
	}
	
	/**
	 * Gets the Set containing the configuration for the providers included in the XML configuration file
	 * 
	 * @return The Set containing the configuration for the providers included in the XML configuration file
	 */
	public Set<ProviderConfiguration> getProviderConfigurations()
	{
		if(providerConfigurations == null)
		{
			providerConfigurations = new HashSet<ProviderConfiguration>();
		}
		
		return providerConfigurations;
	}
	/**
	 * Sets the Set containing the configuration for the providers included in the XML configuration file
	 * 
	 * @param providerConfigurations The Set containing the configuration for the providers included in the XML configuration file
	 * 
	 * @throws IllegalArgumentException If the configurations are invalid
	 */
	public void setProviderConfigurations(Set<ProviderConfiguration> providerConfigurations)
	{
		if(providerConfigurations == null)
			throw new IllegalArgumentException("The provider configurations are invalid");
		
		this.providerConfigurations = providerConfigurations;
	}
	/**
	 * Adds a new configuration to the Set containing the configuration for the providers included in the XML configuration file
	 * 
	 * @param providerConfiguration The configuration to be added to the Set containing the configuration for the providers included in the XML configuration file
	 */
	public void addProviderConfiguration(ProviderConfiguration providerConfiguration)
	{
		getProviderConfigurations().add(providerConfiguration);
	}
	
	/**
	 * Gets the configuration corresponding to the provided provider's name
	 * 
	 * @param providerName Name of the provider of the configuration to be returned
	 * 
	 * @return The configuration corresponding to the provided provider's name
	 * 
	 * @throws IllegalArgumentException if the provided provider's name does not match any of the current configuration items
	 */
	public ProviderConfiguration getProviderConfiguration(String providerName)
	{
		if(providerName == null || providerName.isEmpty())
			throw new IllegalArgumentException("The provided provider name is invalid");
		
		for (ProviderConfiguration item : getProviderConfigurations())
		{
			if(item.getName().equals(providerName))
				return item;
		}
		
		throw new IllegalArgumentException("Invalid provider name");
	}
}
